"""
2、北京高校排名，按“学校名称”、“升序”排序显示所有数据(文件名：高校排名b.py)
	地址：https://www.cnur.com/rankings/188.html
"""
# -*- coding:utf8 -*-

import urllib.request
import pypinyin
import bs4.element
from bs4 import BeautifulSoup

# 链接地址解析-------------------------------------------------------------------------
url = "https://www.cnur.com/rankings/188.html"

HttpResponseObject = urllib.request.urlopen(url)

strHtml = HttpResponseObject.read()
# 构建beautifulsoup实例
soup = BeautifulSoup(strHtml.decode('utf-8'), "lxml")
# 第一个参数是要匹配的内容
# 第二个参数是beautifulsoup要采用的模块，即规则

# 以上都是固定套路

content = []
for tr in soup.find('tbody').children:
    if isinstance(tr, bs4.element.Tag):
        lines = []
        i = 0
        for td in tr:
            if i == 0:  # 去掉原来的名次，方便我们排序
                i = 1
                continue
            lines.append((td.string).strip())
        content.append(lines)
title = content[0]
content = content[1:]

# 排序  高校名称、“升序
content_sort = sorted(content,key=lambda  x:pypinyin.lazy_pinyin(x[0]))		#按【拼音/升序+数字/降序】排序。。

# 打印
for i in title:
    print(i.ljust(10,chr(12288)),end='')
print('\n')

for line in content_sort:
    for i in line:
        print(i.ljust(10,chr(12288)),end='')
    print('\n')